package com.eight.group.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eight.group.entity.PageResult;
import com.eight.group.entity.Result;
import com.eight.group.pojo.Site;
import com.xqx.eight.group.service.SiteService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 地址管理
 */
@RestController
@RequestMapping("/site")
public class SiteController {
    @Reference
    private SiteService siteService;


    /**
     * 查询所有地址用于展示在地图中
     *
     * @return
     */
    @RequestMapping("/findAll.do")
    public Result findAll() {
        try {
            List<Site> mapPoints = siteService.findAll();
            return new Result(true, "查询所有地址成功", mapPoints);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询地址失败");
        }
    }

    /**
     * 分页查询
     * @param map
     * @return
     */
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody Map<String, String> map) {
//        System.out.println(map)
        String currentPage = map.get("currentPage");//当前页
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        String pageSize = map.get("pageSize");//每页显示条数
        if (pageSize == null ||"".equals(pageSize)){
            pageSize="10";
        }
            String queryString = map.get("queryString");//搜索条件
        PageResult page = siteService.findPage(currentPage, pageSize, queryString);

        return page;
    }

    /**
     * 删除地址的方法
     * @param id
     * @return
     */
    @RequestMapping("/deleteById.do")
    public Result deleteById(Integer id){
        boolean flag =siteService.deleteById(id);
        if (flag){
            //删除成功
            return new Result(true,"删除地址成功");
        }else{
            //删除失败
            return new Result(false,"该地址存在订单,删除地址失败");
        }
    }

    /**
     * 添加地址的方法
     * @param site
     * @return
     */
    @RequestMapping("/addAddress.do")
    public Result addAddress(@RequestBody Site site){
        try {
            siteService.addAddress(site);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
        return new Result(true,"添加成功");
    }

    @RequestMapping("/export.do")
    public Result export(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Site> list = siteService.findAll();
        //获得Excel模板文件绝对路径
        String temlateRealPath = req.getSession().getServletContext().getRealPath("template") + File.separator + "address.xlsx";
//        System.out.println(temlateRealPath);
        //读取模板文件创建Excel表格对象
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));

        XSSFSheet sheet = workbook.getSheetAt(0);//第一个分页
        for (int i = 0; i < list.size(); i++) {
            Site site = list.get(i);
            String addressName = site.getAddressName();
            double lng = site.getLng();
            double lat = site.getLat();

            XSSFRow row = sheet.createRow(i+1); //第i行
            row.createCell(0).setCellValue(addressName);//地址

            row.createCell(1).setCellValue(lng);//经度

            row.createCell(2).setCellValue(lat);//纬度
        }

        //通过输出流进行文件下载
        ServletOutputStream out = resp.getOutputStream();
        resp.setContentType("application/vnd.ms-excel");    //代表Excel文件类型
        resp.setHeader("content-Disposition","attachment;filename=site.xlsx");
        workbook.write(out);
        out.flush();
        out.close();
        workbook.close();
        return null;
    }
}
